import axios from "axios";
import { Notification } from 'element-ui';
import router from '@/router/index'

const apiBaseURL = "http://118.195.152.144:8090";
const token = localStorage.getItem('token')

export default apiBaseURL;

// 所有页面表格数据获取 ----- 获取全部数据
export async function getDataInterface(subAdderss, currentPage, PageSize) {
    const requrl = apiBaseURL + subAdderss
    console.log(requrl)
    return new Promise((resolve, reject) => {
        axios({
            url: requrl,
            methods: 'get',
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
                'token': token
            },
            params: {
                page: currentPage,
                pageSize: PageSize
            }
        }).then((response) => {
            console.log("getDataInterface------------")
            console.log(response)
            if (response.status === 200 && response.data.code === 1000) {
                Notification.success({
                    showClose: true,
                    duration: 1500,
                    title: '成功',
                    message: '数据获取成功！'
                });
                resolve(response.data)
            } else if (response.data.code === 1001 || response.data.code === 1002) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '用户名或密码错误,请重新输入！'
                });
            } else if (response.data.code === 1003) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '程序内部异常！'
                });
            } else if (response.data.code === 1004) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '登陆过期，请重新登陆！'
                });
                router.push({ path: '/Login' })
            } else if (response.data.code === 2000) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '登陆超时，请重新登陆！'
                });
                router.push({ path: '/Login' })
            } else if (response.data.code === 5000) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '未登录或登录失效，请重新登录！'
                });
                router.push({ path: '/Login' })
            } else if (response.data.code === 5000) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '操作失败！'
                });
            }
        }).catch(error => {
            console.log("error: " + error)
        })
    })
}

// 所有页面  ----- 类别请求接口
export async function getCategory(subAdderss) {
    const requrl = apiBaseURL + subAdderss
    return new Promise((resolve, reject) => {
        axios({
            url: requrl,
            methods: 'get',
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
                'token': token
            }
        }).then((response) => {
            console.log("getCategory------------")
            console.log(response)
            resolve(response.data)
        }).catch(error => {
            console.log("error: " + error)
        })
    })
}

// 农业技术信息 ----- 搜索数据请求接口
export async function getSearch(subAdderss, currentPage, PageSize, category,context) {
    const requrl = apiBaseURL + subAdderss
    console.log(subAdderss)
    console.log(requrl)
    var param = {
        page: currentPage,
        pageSize: PageSize,
    }
    if (category.lenght !== 0){
        param.category = category
    }
    if (context.lenght !== 0){
        param.context = context
    }
    console.log(param)
    return new Promise((resolve, reject) => {
        axios({
            url: requrl,
            methods: 'get',
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
                'token': token
            },
            params: param
        }).then((response) => {
            console.log("getSearch------------")
            console.log(response)
            if (response.status === 200 && response.data.code === 1000) {
                // Notification.success({
                //     showClose: true,
                //     duration: 1500,
                //     title: '成功',
                //     message: '数据获取成功！'
                // });
                resolve(response.data)
            } else if (response.data.code === 1003) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '程序内部异常！'
                });
            } else if (response.data.code === 1004) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '登陆过期，请重新登陆！'
                });
                router.push({ path: '/Login' })
            } else if (response.data.code === 2000) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '登陆超时，请重新登陆！'
                });
                router.push({ path: '/Login' })
            } else if (response.data.code === 5000) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '未登录或登录失效，请重新登录！'
                });
                router.push({ path: '/Login' })
            } else if (response.data.code === 5000) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '操作失败！'
                });
            }
        }).catch(error => {
            console.log("error: " + error)
        })
    })
}


//肥料部级备案数据 ----- 通过两个下拉菜单选择的数据请求数据，两个数据都是可选数据
export async function getDataSearch(subAdderss, currentPage, PageSize, Genproname, Promodal) {
    const apiBaseURL = "http://118.195.152.144:8090";
    const requrl = apiBaseURL + subAdderss
    console.log(subAdderss)
    console.log(requrl)
    var param = {
        page: currentPage,
        pageSize: PageSize,
    }
    if (Genproname.lenght !== 0){
        param.Genproname = Genproname
    }
    if (Promodal.lenght !== 0){
        param.Promodal = Promodal
    }
    console.log('param:----------------------')
    console.log(param)
    return new Promise((resolve, reject) => {
        axios({
            url: requrl,
            methods: 'get',
            headers: {
                'Content-Type': 'application/json;charset=UTF-8',
                'token': token
            },
            params: param
        }).then((response) => {
            console.log("getSearch------------")
            console.log(response)
            if (response.status === 200 && response.data.code === 1000) {
                Notification.success({
                    showClose: true,
                    duration: 1500,
                    title: '成功',
                    message: '数据获取成功！'
                });
                resolve(response.data)
            } else if (response.data.code === 1003) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '程序内部异常！'
                });
            } else if (response.data.code === 1004) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '登陆过期，请重新登陆！'
                });
                router.push({ path: '/Login' })
            } else if (response.data.code === 2000) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '登陆超时，请重新登陆！'
                });
                router.push({ path: '/Login' })
            } else if (response.data.code === 5000) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '未登录或登录失效，请重新登录！'
                });
                router.push({ path: '/Login' })
            } else if (response.data.code === 5000) {
                Notification.error({
                    showClose: true,
                    duration: 1500,
                    title: '错误',
                    message: '操作失败！'
                });
                router.push({ path: '/Login' })
            }
        }).catch(error => {
            console.log("error: " + error)
        })
    })
}
